﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ORSLibrary.DAL;
using ORSLibrary;

namespace Website.ar.UserControls
{
    public partial class UC_Company : System.Web.UI.UserControl
    {
        Company _object;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = "";

            if (!IsPostBack)
            {
                Session.Remove("_Company");
                pgvwList.Selected = true;
            }
            else
            {
                _object = (Company)Session["_Company"];
            }
        }
        protected void btnNew_Click(object sender, EventArgs e)
        {
            New();

            pgvwForm.Selected = true;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Save()) pgvwList.Selected = true;
        }
        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (Save()) New();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("_Company");

            pgvwList.Selected = true;
        }
        protected void btnDeleteMulti_Click(object sender, EventArgs e)
        {
            bool err = false;
            int count = 0;

            foreach (Telerik.Web.UI.GridItem item in grid.SelectedItems)
            {
                try
                {
                    Company.Delete(int.Parse(item.Cells[3].Text));
                }
                catch (Exception)
                {
                    err = true;
                    count++;
                }
            }

            if (err && grid.SelectedItems.Count == count)
                Utilities.ShowMessage(lblMessage, "Deleting failed", Utilities.MessageType.error);
            else if (err)
                Utilities.ShowMessage(lblMessage, "Deleted complete, but not for all", Utilities.MessageType.warning);
            else
                Utilities.ShowMessage(lblMessage, "Deleted successfully", Utilities.MessageType.success);

            grid.DataBind();
        }
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Company.Delete(int.Parse(((ImageButton)sender).CommandArgument));

                Utilities.ShowMessage(lblMessage, "Deleted successfully", Utilities.MessageType.success);
            }
            catch (Exception ex)
            {
                Utilities.ShowMessage(lblMessage, "Deleting failed: " + ex.Message, Utilities.MessageType.error);
            }

            grid.DataBind();
        }
        protected void btnEdit_Click(object sender, ImageClickEventArgs e)
        {
            Session["_Company"] = _object = new Company(int.Parse(((ImageButton)sender).CommandArgument));

            Bind();

            pgvwForm.Selected = true;
        }

        private void New()
        {
            _object = new Company();

            Session["_Company"] = _object;

            Bind();
        }
        private bool Save()
        {
            try
            {
                UnBind();

                _object.Save();

                Utilities.ShowMessage(lblMessage, "Saved successfully", Utilities.MessageType.success);

                grid.DataBind();

                return true;
            }
            catch (Exception ex)
            {
                Utilities.ShowMessage(lblMessage, "Saving failed: " + ex.Message, Utilities.MessageType.error);

                return false;
            }
        }
        private void Bind()
        {
            //Bind Data
            txtNameAr.Text = _object.NameAr;
            txtNameEn.Text = _object.NameEn;
        }
        private void UnBind()
        {
            //Bind Data
            _object.NameAr = txtNameAr.Text;
            _object.NameEn = txtNameEn.Text;
        }
    }
}